package com.chenyi.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

/**
 * [234]回文链表
 *
 * @author by chenyi
 * @className PalindromeLinkedList
 * @date 2022-04-14 19:55:18
 */
public class PalindromeLinkedList {
    public static void main(String[] args) {
        Solution solution = new PalindromeLinkedList().new Solution();
        ListNode listNode = new ListNode(1);

        ListNode listNode1 = new ListNode(2);
        listNode.next = listNode1;

        ListNode listNode2 = new ListNode(2);
        listNode1.next = listNode2;

        ListNode listNode3 = new ListNode(1);
        listNode2.next = listNode3;


        System.out.println(solution.isPalindrome(listNode));
    }

    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
            //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
    class Solution {
        public boolean isPalindrome(ListNode head) {
            List<Integer> list = new ArrayList<>();

            while (head != null) {
                list.add(head.val);
                head = head.next;
            }
            int first = 0;
            int end = list.size() - 1;
            while (first < end) {
                if (!list.get(first).equals(list.get(end))){
                    return false;
                }
                first++;
                end--;
            }
            return true;
        }
    }

    //leetcode submit region end(Prohibit modification and deletion)
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }


}
